package com.recruit.wechat.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

import com.recruit.wechat.bean.Role;
import com.recruit.wechat.bean.result.ResultPage;
import com.recruit.wechat.controller.helper.$;
import com.recruit.wechat.controller.model.ApiResultModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2019-09-03
 */
@Controller
@RequestMapping("/WeChatUser/role")
@Slf4j
public class RoleController extends BaseController{
     /** 主页面 */
    @RequestMapping("admin/main.html")
    public String main(){
       return  "role/RoleMain";
    }
    /** 添加页面跳转
     * @return 返回页面
     */
    @RequestMapping(value = "admin/addRoleHtml")
    public String addRoleHtml( ){
             return "role/RoleAdd";
    }
    /** 编辑页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/editRoleHtml")
    public String editRoleHtml(Integer id ,Model model ){
           model.addAttribute("role",roleService.selectRoleByIdRole(id));
            List<String> list=rolePermissionService.findRolePermissionIds(id);
            String permission=  CollUtil.join(list, ",");
            log.debug("角色{}的菜单ID为：{}",id,permission);
             model.addAttribute("permissionIds",permission);
             return "role/RoleEdit";
    }

    /** 查看页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/showRoleHtml")
    public String showRoleHtml(Integer id ,Model model ){
        model.addAttribute("role",roleService.selectRoleByIdRole(id));
        List<String> list=rolePermissionService.findRolePermissionIds(id);
        String permission=  CollUtil.join(list, ",");
        log.debug("角色{}的菜单ID为：{}",id,permission);
        model.addAttribute("permissionIds",permission);
             return "role/RoleShow";
    }


    /** 分页查询Role
     * @param page  页码
     * @param limit  查几条
     * @param role  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "admin/findRoleByPage")
    @ResponseBody
    public ResultPage findRoleByPage(Integer page, Integer limit, Role role){
             return roleService.findRoleByPage(page,limit,role);
    }

    /** 添加Role
     * @param role  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/addRole")
    @ResponseBody
    public ApiResultModel saveRole(Role role , String permissionIds ){
                if(StrUtil.hasBlank(permissionIds)){
                    return $.error("请选择权限");
                }
             return roleService.addRole(role,permissionIds);
    }

    /** 编辑Role
     * @param role  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/editRole")
    @ResponseBody
    public ApiResultModel editRole(Role role , String permissionIds){
             return roleService.editRole(role,permissionIds);
    }



    /** 删除一条Role
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/delRole")
    @ResponseBody
    public ApiResultModel delRole( Integer id  ){
             return roleService.delRole(id);
    }


    /** 查询菜单树
     */
    @RequestMapping(value = "admin/finPermissionAjax")
    @ResponseBody
    public ApiResultModel finPermissionAjax(   ){
        return $.success(permissionService.finPermissionAjax());
    }


    /** 查询所有角色
     */
    @RequestMapping(value = "admin/finRoleAjax")
    @ResponseBody
    public ApiResultModel finRoleAjax(   ){
        return $.success(roleService.list());
    }



}
